package com.kzd.gtboon.main.game.adapter

import android.graphics.Rect
import android.text.TextUtils
import android.view.View
import android.view.ViewTreeObserver
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kzd.gtboon.R
import com.kzd.gtboon.entity.*

const val ITEM_DETAILS_NULL = 0
const val ITEM_DETAILS_TEXT = 1
const val ITEM_DETAILS_GRAPHIC = 2
const val ITEM_DETAILS_GUESS = 3
const val ITEM_DETAILS_COUPON = 4
const val ITEM_DETAILS_DOING = 5
const val ITEM_DETAILS_GIFT = 6

class DetailsAdapter1 : BaseMultiItemQuickAdapter<GameDetailsItem<*>, BaseViewHolder>() {

    init {
        addItemType(ITEM_DETAILS_NULL, R.layout.item_game_details_null)
        addItemType(ITEM_DETAILS_TEXT, R.layout.item_game_details_text)
        addItemType(ITEM_DETAILS_GRAPHIC, R.layout.item_game_details_graphic)
        addItemType(ITEM_DETAILS_GUESS, R.layout.item_game_details_list)
        addItemType(ITEM_DETAILS_COUPON, R.layout.item_game_details_coupon)
        addItemType(ITEM_DETAILS_DOING, R.layout.item_game_details_list)
        addItemType(ITEM_DETAILS_GIFT, R.layout.item_game_details_list)
        addChildClickViewIds(R.id.tvGameDetailsGraphicVip,R.id.tvGameDetailsNullQQ)
    }

    private var mOnItemClickSunListener: ((BaseQuickAdapter<*, *>, View, Int, Any) -> Unit)? = null

    fun setOnItemClickSunListener(listener: ((BaseQuickAdapter<*, *>, View, Int, Any) -> Unit)) {
        this.mOnItemClickSunListener = listener
    }

    override fun convert(holder: BaseViewHolder, item: GameDetailsItem<*>) {
        when (holder.itemViewType) {
            ITEM_DETAILS_NULL ->{
                holder.setText(R.id.tvGameDetailsNullTitle, item.title)
                    .setText(R.id.tvGameDetailsNullRebate, item.text?.replace(("\r"), ("\n")))
            }
            ITEM_DETAILS_TEXT -> {
                holder.setText(R.id.tvGameDetailsTextTitle, item.title)
                    .setText(R.id.tvGameDetailsTextRebate, item.text?.replace(("\r"), ("\n")))

                val tvCheckBox = holder.getView<CheckBox>(R.id.cbGameDetailsTextRebateOpen)

                val tvGameDetailsTextRebate =
                    holder.getView<TextView>(R.id.tvGameDetailsTextRebate)
                tvGameDetailsTextRebate.viewTreeObserver.addOnGlobalLayoutListener(
                    object : ViewTreeObserver.OnGlobalLayoutListener {
                        override fun onGlobalLayout() {
                            val lineCount = tvGameDetailsTextRebate.lineCount
                            if (lineCount <= 4) {
                                tvCheckBox.visibility = View.INVISIBLE
                                tvGameDetailsTextRebate.maxLines = Int.MAX_VALUE
                            } else {
                                tvCheckBox.visibility = View.VISIBLE
                                tvGameDetailsTextRebate.maxLines = 4
                                tvGameDetailsTextRebate.ellipsize = TextUtils.TruncateAt.END
                            }
                            tvGameDetailsTextRebate.viewTreeObserver
                                .removeOnGlobalLayoutListener((this))
                        }
                    })

                tvCheckBox.setOnCheckedChangeListener { buttonView, isChecked ->
                    if (isChecked) {
                        buttonView.text = "收起"
                        tvGameDetailsTextRebate.maxLines = Int.MAX_VALUE
                    } else {
                        buttonView.text = "展开"
                        tvGameDetailsTextRebate.maxLines = 4
                        tvGameDetailsTextRebate.ellipsize = TextUtils.TruncateAt.END
                    }
                }
            }
            ITEM_DETAILS_GRAPHIC -> {
                holder.setText(R.id.tvGameDetailsGraphicVip, item.title)
                    .setText(R.id.tvGameDetailsGraphicContent, item.text?.replace("\r", "\n"))
                    .getView<RecyclerView>(R.id.rvGameDetailsGraphicImageBody).apply {
                        layoutManager = LinearLayoutManager(context).apply {
                            orientation = LinearLayoutManager.HORIZONTAL
                        }
                        if (itemDecorationCount > 0)
                            return@apply
                        addItemDecoration(ItemDecoration())
                    }.adapter = GameDetailsImageAdapter().apply {
                    setNewInstance(item.getConversionList(String::class.java))
                }
                val tvCheckBox = holder.getView<CheckBox>(R.id.cbGameDetailsGraphicContentOpen)

                val tvGameDetailsGraphicContent =
                    holder.getView<TextView>(R.id.tvGameDetailsGraphicContent)
                tvGameDetailsGraphicContent.viewTreeObserver.addOnGlobalLayoutListener(
                    object : ViewTreeObserver.OnGlobalLayoutListener {
                        override fun onGlobalLayout() {
                            val lineCount = tvGameDetailsGraphicContent.lineCount
                            if (lineCount <= 4) {
                                tvCheckBox.visibility = View.GONE
                                tvGameDetailsGraphicContent.maxLines = Int.MAX_VALUE
                            } else {
                                tvCheckBox.visibility = View.VISIBLE
                                tvGameDetailsGraphicContent.maxLines = 4
                                tvGameDetailsGraphicContent.ellipsize = TextUtils.TruncateAt.END
                            }

                            tvGameDetailsGraphicContent.viewTreeObserver
                                .removeOnGlobalLayoutListener((this))
                        }
                    })

                tvCheckBox.setOnCheckedChangeListener { buttonView, isChecked ->
                    if (isChecked) {
                        buttonView.text = "收起"
                        tvGameDetailsGraphicContent.maxLines = Int.MAX_VALUE
                    } else {
                        buttonView.text = "展开"
                        tvGameDetailsGraphicContent.maxLines = 4
                        tvGameDetailsGraphicContent.ellipsize = TextUtils.TruncateAt.END
                    }
                }

            }
            ITEM_DETAILS_GUESS -> {
                holder.setText(R.id.tvGameDetailsListTitle, item.title)
                    .getView<RecyclerView>(R.id.rvGameDetailsListBody).apply {
                        this.layoutManager = GridLayoutManager(context, (3))
                    }.adapter = Game3Adapter().apply {
                    setNewInstance(item.getConversionList(GameBean::class.java))
                }
            }
            ITEM_DETAILS_COUPON -> {
                holder.getView<RecyclerView>(R.id.rvGameDetailsCouponBody).apply {
                    this.layoutManager = LinearLayoutManager(context).apply {
                        orientation = LinearLayoutManager.HORIZONTAL
                    }
                    if (itemDecorationCount > 0) {
                        return@apply
                    }
                    addItemDecoration(ItemDecoration())
                }.adapter = GameWelfareCouponAdapter().apply {
                    setNewInstance(item.getConversionList(WelfareCoupon::class.java))
                    setOnItemClickListener { _, _, _ ->
                        holder.itemView.performClick()
                    }
                }
            }
            ITEM_DETAILS_DOING -> {
                holder.setText(R.id.tvGameDetailsListTitle, item.title)
                    .getView<RecyclerView>(R.id.rvGameDetailsListBody).apply {
                        this.layoutManager = LinearLayoutManager(context)
                    }.adapter = GameWelfareDoingAdapter().apply {
                    val itemList = item.getConversionList(WelfareDoing::class.java)
                    setNewInstance(itemList)
                }
            }
            ITEM_DETAILS_GIFT -> {
                holder.setText(R.id.tvGameDetailsListTitle, item.title)
                    .getView<RecyclerView>(R.id.rvGameDetailsListBody).apply {
                        this.layoutManager = LinearLayoutManager(context)
                    }.adapter = GameWelfareGiftAdapter().apply {
                    setNewInstance(item.getConversionList(WelfareGift::class.java))
                    setOnItemChildClickListener { adapter, view, position ->
                        val itemData = getItem(position)
                        mOnItemClickSunListener?.invoke(adapter, view, position, itemData)
                    }
                }
            }
        }
        holder.itemView.tag = holder.layoutPosition
    }

    inner class ItemDecoration : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
        ) {
            val position: Int = parent.getChildAdapterPosition(view)
            if (position == 0) {
                outRect.left = ConvertUtils.dp2px(15f)
            }
            if (position == state.itemCount - 1) {
                outRect.right = ConvertUtils.dp2px(15f)
            } else {
                outRect.right = ConvertUtils.dp2px(7f)
            }
            outRect.top = ConvertUtils.dp2px(10f)
            outRect.bottom = ConvertUtils.dp2px(10f)
        }
    }
}
